﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Littlefish.Validation
{
    public class ErrorHandler
    {
        #region - Constructors -
        public ErrorHandler() : this(false) { }
        public ErrorHandler(bool throwExceptions)
        {
            this.ThrowExceptions = throwExceptions;
        }
        #endregion

        #region - Public Properties  -
        /// <summary>
        /// Specifieds whether exceptions are thrown.
        /// </summary>
        public bool ThrowExceptions { get; set; }

        /// <summary>
        /// Specifies whether exceptions will be logged using
        /// the <see cref="ILogErrors"/> instance stored in the <see cref="Logger"/> property.
        /// </summary>
        public bool LogExceptions { get; set; }

        /// <summary>
        /// Mechanism used to log errors if <see cref="LogExceptions"/> is set to true.
        /// </summary>
        public ILogErrors Logger { get; set; }

        private Exception m_errorException;
        /// <summary>
        /// Instance of an exception object that caused the last error
        /// </summary>                
        public Exception ErrorException
        {
            get { return ErrorException; }
            set 
            { 
                this.m_errorException = value;

                if (value != null)
                {
                    if (LogExceptions && Logger != null)
                        Logger.LogException(value);

                    if (this.ThrowExceptions)
                        throw value;
                }
            }
        }

        /// <summary>
        /// Error Message of the last exception
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                if (this.ErrorException == null)
                    return "";
                return this.ErrorException.Message;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                    this.ErrorException = null;
                else
                    // *** Assign a new exception
                    this.ErrorException = new ApplicationException(value);
            }
        }
        #endregion

        #region - Public Methods -
        /// <summary>
        /// Sets an internal error message.
        /// </summary>
        /// <param name="Message"></param>
        public void SetError(string Message)
        {
            if (string.IsNullOrEmpty(Message))
                this.ErrorException = null;
            else
                this.ErrorException = new ApplicationException(Message);
        }

        /// <summary>
        /// Sets an internal error exception
        /// </summary>
        /// <param name="ex"></param>
        public void SetError(Exception ex)
        {
            this.ErrorException = ex;
        }

        /// <summary>
        /// Clear out errors
        /// </summary>
        public void SetError()
        {
            this.ErrorException = null;
        }
        #endregion
    }
}
